
package userBuilder;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class NewMain {


    public static void main(String[] args) {
        try {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.newDocument();
            
            //Tao doi tuong file, add content

            Node newItem = doc.createElement("acc");
            Node newName = doc.createElement("name");
            Node newPass = doc.createElement("pass");

            newName.setTextContent("romantic9x");
            newPass.setTextContent("123");

            newItem.appendChild(newName);
            newItem.appendChild(newPass);

            //get element of file and add child
            //doc.getDocumentElement().appendChild(newItem);
            doc.appendChild(newItem);
            Source source = new DOMSource(doc);

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            Result result = new StreamResult(new File("account_1.xml"));
            System.out.println((new File("account_1.xml").getAbsolutePath()));
            transformer.transform(source, result);
            System.out.println("Success!");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

